package com.doophan.dac.client.service.impl;

import com.doophan.dac.client.domain.TreeNode;
import com.doophan.dac.client.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FileServiceImpl implements FileService {
    @Override
    public List<TreeNode> getChildren(String path) throws Exception {
        path = URLDecoder.decode(path, "utf-8");
        List<TreeNode> children = new ArrayList<>();
        File f = new File(path);
        for(File child : f.listFiles()) {
            TreeNode treeNode = new TreeNode();
            treeNode.setId(child.getAbsolutePath());
            treeNode.setLabel(child.getName());
            treeNode.setKey(child.getAbsolutePath());
            treeNode.setIsLeaf(child.isFile());
            children.add(treeNode);
        }
        return children;
    }
}
